/*
 * Copyright (C) Beijing Decision Credit Technologies, Ltd
 *
 * ProjectName:Kashen
 * PackageName:com.woaika.kashen
 * FileName:WIKCoreConfig.java
 * 
 */

package com.qdaily.ui.data;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * @Description: 全局配置管理类
 * @Author: Felix
 * @Date: 2015年04月14日下午2:50:51
 * @Version:1.0
 */
public class QDConfigManager {

    private static final String TAG = "QDConfigManager";
    private static final String CONFIG_NAME = "qdaily";

    private static QDConfigManager instance;
    private Context mContext;

    /**
     * 标记是否是否显示过引导页面
     */
    public final static String KEY_HAS_SHOW_GUIDE = "KEY_HAS_SHOW_GUIDE";
    public static final boolean DEFAULT_KEY_HAS_SHOW_GUIDE = false;

    private QDConfigManager() {
    }

    /**
     * @return the instance
     */
    public static QDConfigManager getInstance() {
        if (instance == null) {
            instance = new QDConfigManager();
        }
        return instance;
    }

    /**
     * @param context
     * @Description 初始化配置
     */
    public void initConfig(Context context) {
        mContext = context;
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        SharedPreferences.Editor editor = settings.edit();
        editor.commit();
    }

    public boolean saveString(String key, String value) {
        if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
            return false;
        }
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public String getString(String key, String defaultValue) {
        if (TextUtils.isEmpty(key)) {
            return "";
        }
        String value = "";
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        value = settings.getString(key, defaultValue);
        return value;
    }

    public boolean saveBoolean(String key, boolean value) {
        if (TextUtils.isEmpty(key)) {
            return false;
        }
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        if (TextUtils.isEmpty(key)) {
            return false;
        }
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        return settings.getBoolean(key, defaultValue);
    }

    public boolean saveInt(String key, int value) {
        if (TextUtils.isEmpty(key)) {
            return false;
        }
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public int getInt(String key, int defaultValue) {
        if (TextUtils.isEmpty(key)) {
            return -1;
        }
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        return settings.getInt(key, defaultValue);
    }

    public boolean saveLong(String key, long value) {
        if (TextUtils.isEmpty(key)) {
            return false;
        }
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public long getLong(String key, long defaultValue) {
        if (TextUtils.isEmpty(key)) {
            return -1;
        }
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        return settings.getLong(key, defaultValue);
    }

    public void remove(String key) {
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        SharedPreferences.Editor editor = settings.edit();
        editor.remove(key);
        editor.commit();
    }

    public void removeAll() {
        SharedPreferences settings = mContext.getSharedPreferences(CONFIG_NAME,
                0);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();
    }
}
